<?php

namespace app\controller\adminapi\cloud;

use app\common\model\platform\Platform;
use app\common\model\platform\PlatformApp;
use app\common\model\system\Config;
use hiduoke\controller\BaseController;

/**
 * Notes: 外部请求当前系统同步数据
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 * Created on:  2025/8/6 03:18
 */
class Local extends BaseController
{
    public $notLogin = ['*'];
    public $notCheck = ['*'];

    public function initialize()
    {
        parent::initialize();
        $cloud = Config::get("cloud");
        $site_id = input('site_id', 0);
        $site_token = input('site_token', '');
        if ($site_id != $cloud['site_id'] || $site_token != $cloud['token']) {
            $this->error('站点信息或者接入秘钥错误')->send();
        }
    }

    public function getPlatformMsg()
    {
        $type = input('type');
        $token = input('token');
        if ($type == 2) {
            $res = (new PlatformApp())->where(['token' => $token])->field(['id', 'name', 'plaid', 'logo'])->find();
            if (!$res) {
                return $this->error('小程序不存在');
            }
            $platform = (new Platform())->where(['plaid' => $res['plaid']])->field(['name', 'logo'])->find();
            $res['platform'] = $platform;
            $data = array(
                'appid'    => $res['id'],
                'plaid'    => $res['plaid'],
                'app_name' => $res['logo'],
                'app_logo' => $res['logo'],
                'name'     => $platform['name'],
                'logo'     => $platform['logo']
            );

            return $this->success('获取成功', $data);
        } else {
            // 屏蔽平台敏感信息
            $res = (new Platform())->where(['token' => $token])->hidden(['corp_id', 'config'])->find();
            if (!$res) {
                return $this->error('平台不存在');
            }
            return $this->success('获取成功', $res);
        }
    }

}
